using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.TestDrivens;
using Pixysoft.Framework.Logs.Core;
using Pixysoft.Framework.Configurations;

namespace Pixysoft.Framework.Logs.TestCases
{
    [TestDriven(TestDrivenType.IntegrationTest, "LogReport")]
    class UnitTest_003_LogReport
    {
        private static IAssert assert = Assert.New(true);

        [TestCase("RawSerialize")]
        public static void TestCase_000_InitializeReport()
        {
            string id = StringHelper.GetId();
            string date = StringHelper.GetRawDateTime();
            DateTime datetime = DateTime.MinValue;
            string ip = "0.0.0.1";

            string dll = "pixysoft.dll";
            string _class = "Console";
            string method = "Write";
            string file = dll;
            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            string line = "1";
            string column = "1";

            LogDocument doc = new LogDocument(XmlManager.CreateNode(StringHelper.default_element_document));
            doc.Filename = "pixysoft.log";
            doc.Filepath = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            assert.IsEqual("pixysoft.log", doc.Filename);
            assert.IsEqual(StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory), doc.Filepath);

            ILogRecord record = doc.Records.Add(id, date, LogLevel.ALL, "helloworld", ip);
            record.Filepath = path;
            record.Text = "helloworld";
            assert.IsEqual(id, record.Id);
            assert.IsEqual(date, record.RawDate);
            assert.Write(record.Date);
            assert.IsEqual(path, record.Filepath);
            assert.IsEqual(ip, record.Ip);
            assert.IsEqual(LogLevel.ALL, record.Level);
            assert.IsEqual("helloworld", record.Text);


            ILogFrame frame = record.Frames.Add(dll, _class, method, file, path, line, column);
            assert.IsEqual(dll, frame.Dll);
            assert.IsEqual(_class, frame.Class);
            assert.IsEqual(method, frame.Method);
            assert.IsEqual(file, frame.File);
            assert.IsEqual(path, frame.Path);
            assert.IsEqual(line, frame.Line);
            assert.IsEqual(column, frame.Column);


            LogReport report = new LogReport();
            report.Id = id;
            report.TimeFrom = datetime;
            report.TimeTo = datetime;
            report.TimeSpan = 1;
            report.Document = doc;
            report.Record = record;
            report.FrameDocuments = new ILogDocument[] { doc };
            assert.IsEqual(id, report.Id);
            assert.IsEqual(datetime, report.TimeFrom);
            assert.IsEqual(datetime, report.TimeTo);
            assert.IsEqual(1, report.TimeSpan);
            assert.IsNotNull(report.Document);
            assert.IsEqual(doc.Records.Count, report.Document.Records.Count);
            assert.IsNotNull(report.Record);
            assert.IsEqual(record.Frames.Count, report.Record.Frames.Count);
            assert.IsNotNull(report.FrameDocuments);
            assert.IsEqual(doc.Records.Count, report.FrameDocuments[0].Records.Count);


            Assert.Cache.Push(report);

        }

        [TestCase("Serialize")]
        public static void TestCase_001_Serialize()
        {
            Assert.Call(UnitTest_003_LogReport.TestCase_000_InitializeReport);

            ILogReport report = Assert.Cache.Pop<ILogReport>();

            string content = report.Serialize();

            assert.Write(content);
            assert.Write(report.Serialize(true));
            assert.Write(report.RawSerialize());

            // test case for LogReport.Serialize;

        }

        [TestCase("Serialize")]
        public static void TestCase_002_TestEntity()
        {
            Assert.Call(UnitTest_003_LogReport.TestCase_000_InitializeReport);

            ILogReport report = Assert.Cache.Pop<ILogReport>();

            assert.Write("---------------------------------");

            foreach (ILogDocument doc in report.FrameDocuments)
            {
                foreach (ILogRecord record in doc.Records)
                {
                    foreach (ILogFrame frame1 in record.Frames)
                    {
                        assert.Write(frame1.Dll);
                    }
                }
            }

            ILogRecord rec = report.Document.Records[0];
            ILogFrame frame = rec.Frames[0];
            string recid = rec.Id;

            assert.IsEqual(recid, report.Document.Records[recid].Id);
            assert.IsEqual(recid, report.Document.Records[0].Id);
            assert.IsEqual(1, report.Document.Records.Count);
            assert.IsTrue(report.Document.Records.Contains(recid));
            report.Document.Records.Sort();
            assert.IsEqual(1, report.Document.Records.Count);
            report.Document.Records.Clear();
            assert.IsEqual(0, report.Document.Records.Count);
            report.Document.Records.Add(rec);
            assert.IsEqual(1, report.Document.Records.Count);


            assert.IsEqual(frame.Dll, rec.Frames[0].Dll);
            assert.IsEqual(1, rec.Frames.Count);

        }

    }
}
